package com.cx.controller.h5;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.cx.base.ApiModel;
import org.cx.base.BaseController;
import org.cx.entity.PageData;
import org.cx.util.HttpsUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.cx.service.RedisService;
import com.cx.service.activity.ActivityManager;

@RestController("h5/index")
@RequestMapping("h5/index")
public class IndexController extends BaseController {

	@Resource
	private RedisService redisService;

	@Resource
	private ActivityManager activityService;

	/**
	 * 活动入口
	 * 
	 * 第三方接口返回用户基本信息json格式
	 * {"memberid": "123456",
	    "membername": "会员名称",
	    "memberphone": "13560294635",
	    "memberheadurl": "http://www.baidu.com",
	    "clerkid": "123456",
	    "clerkname": "店员名称",
	    "clerkphone": "13560294635",
	    "clerkheadurl": "http://www.baidu.com",
	    "storeid": "123456",
	    "storename": "门店",
	    "p3id": "123456",
	    "p3name": "三级连锁",
	    "p2id": "123456",
	    "p2name": "二级连锁",
	    "p1id": "123456",
	    "p1name": "一级经销商",
	    "regionid": "123456",
	    "regionname": "区域",
	    "districtid": "123456",
	    "districtname": "大区"}
	 * 
	 * */
	@ResponseBody
	@RequestMapping(value = "entrance", produces = "application/json;charset=UTF-8")
	public ApiModel entrance() throws Exception {
		PageData pd = new PageData();
		pd = this.getPageData();
		//生成唯一token
		String token = UUID.randomUUID().toString().replaceAll("-", "");
		//传入唯一标识
		String code = pd.getString("code");
		//传入活动ID
		String actidStr = pd.getString("actid");
		if(actidStr != null && !actidStr.equals("")){
			String key = "ACTIVITYID_"+actidStr;
			String url = (String)redisService.get(key);
			if(url == null || "".equals(url)){
				PageData activity = activityService.selectByPrimaryKey(pd);
				url = activity.getString("userurl");
				//存缓存
				redisService.set(key, url, 60 * 60L);
			}
			//获取用户信息 存redis
			Map<String, String> pmap = new HashMap<String, String>();
			pmap.put("code", code);
			String result = HttpsUtil.sendHttpsGet(url, new HashMap<String, String>(), pmap, false, null, null);
			//增加活动ID，token
			JSONObject resultObj = JSONObject.parseObject(result);
			resultObj.put("token", token);
			resultObj.put("actid", actidStr);
			result = resultObj.toJSONString();
			
			//用户信息存缓存
			redisService.set(token, result, 30 * 60L);
			
			return buildSuccMsg(resultObj);
		}
		return buildErrorMsg("获取用户信息出错，请退出重新进来。");
	}

	/**
	 * 模拟用户
	 * */
	@ResponseBody
	@RequestMapping(value = "getFakeUser", produces = "application/json;charset=UTF-8")
	public String getFakeUser() throws Exception {
		String str = "{\"memberid\": \"123456\",\"membername\": \"会员名称\",\"memberphone\": \"13560294635\",\"memberheadurl\": \"http://www.baidu.com\","+
	    "\"clerkid\": \"123456\",\"clerkname\": \"店员名称\",\"clerkphone\": \"13560294635\",\"clerkheadurl\": \"http://www.baidu.com\","+
	    "\"storeid\": \"123456\",\"storename\": \"门店\","+
	    "\"p3id\": \"123456\",\"p3name\": \"三级连锁\","+
	    "\"p2id\": \"123456\",\"p2name\": \"二级连锁\","+
	    "\"p1id\": \"123456\",\"p1name\": \"一级经销商\","+
	    "\"regionid\": \"123456\",\"regionname\": \"区域\","+
	    "\"districtid\": \"123456\",\"districtname\": \"大区\"}";
		return str;
	}
}